package com.bigdata.service.impl;

import com.bigdata.dao.DataConnectionMapper;
import com.bigdata.entity.pojo.Location;
import com.bigdata.entity.pojo.StatusOfLocation;
import com.bigdata.entity.pojo.TypeAndStatus;
import com.bigdata.entity.vo.DataConnectionRateVO;
import com.bigdata.entity.vo.DataConnectionVO;
import com.bigdata.service.DataConnectionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataConnectionServiceImpl implements DataConnectionService {
    @Autowired
    private DataConnectionMapper dataConnectionMapper;
    @Override
    public DataConnectionVO getAllDataConnections() {
        List<StatusOfLocation> statusOfLocations = dataConnectionMapper.getAllStatusOfLocation();
//        List<Location> success = new ArrayList<>();
//        List<Location> failure = new ArrayList<>();
//        for (StatusOfLocation statusOfLocation : statusOfLocations) {
//            Location location = new Location();
//            BeanUtils.copyProperties(statusOfLocation, location);
//            if (statusOfLocation.getStatus().equals("11111")) {
//
//                success.add(location);
//            } else {
//                failure.add(location);
//            }
//        }
//
//        DataConnectionVO dataConnectionVO = new DataConnectionVO();
//        dataConnectionVO.setSuccess(success);
//        dataConnectionVO.setFailure(failure);
//        return dataConnectionVO;
        // 创建两个列表分别保存成功和失败的 Location 对象
        List<Location> success = new ArrayList<>();
        List<Location> failure = new ArrayList<>();

        // 遍历 StatusOfLocation，将符合条件的数据添加到 success 或 failure 列表中
        for (StatusOfLocation statusOfLocation : statusOfLocations) {
            Location location = new Location();
            // 复制经纬度数据
            location.setLongitude(statusOfLocation.getLongitude());
            location.setLatitude(statusOfLocation.getLatitude());

            // 根据 status 字段判断成功或失败
            if (statusOfLocation.getStatus().equals("11111")) {
                success.add(location);
            } else {
                failure.add(location);
            }
        }

        // 将结果封装成 DataConnectionVO 对象
        DataConnectionVO dataConnectionVO = new DataConnectionVO();
        dataConnectionVO.setSuccess(success);
        dataConnectionVO.setFailure(failure);

        return dataConnectionVO;
    }

    @Override
    public DataConnectionRateVO getAllDataConnectionRates() {
        List<String>typeName=new ArrayList<>();
        typeName.add("CMCC");
        typeName.add("CUCC");
        typeName.add("CTCC");
        Map<String,Map<String,Double>>map=new HashMap<>();
        List<TypeAndStatus>typeAndStatuses=dataConnectionMapper.getAllTypeAndStatus();
        System.out.println(typeAndStatuses);
        for(TypeAndStatus typeAndStatus:typeAndStatuses){
            Map<String,Double> temp=map.getOrDefault(typeAndStatus.getType(),new HashMap<>());
            double count=temp.getOrDefault(typeAndStatus.getStatus(),Double.valueOf(0))+1;
            temp.put(typeAndStatus.getStatus(),count);
            map.put(typeAndStatus.getType(),temp);
        }
        System.out.println(map);
        List<Double>rate=new ArrayList<>();
        for(String name:typeName) {
            double success = map.get(name).get("11111");
            double failure = map.get(name).get("00000");

                double result = success / (success + failure);
                rate.add(result);
            }

        DataConnectionRateVO dataConnectionRateVO = new DataConnectionRateVO();
        dataConnectionRateVO.setXAxisData(typeName);
        dataConnectionRateVO.setSeriesData(rate);
        return dataConnectionRateVO;
    }
}
